package tv.huan.web.controller.media;

import java.util.List;

import io.swagger.models.auth.In;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import tv.huan.common.annotation.Log;
import tv.huan.common.core.controller.BaseController;
import tv.huan.common.core.domain.AjaxResult;
import tv.huan.common.core.page.TableDataInfo;
import tv.huan.common.enums.BusinessType;
import tv.huan.media.domain.Team;
import tv.huan.media.service.IAlbumService;
import tv.huan.media.service.ITeamService;
import tv.huan.system.domain.SysUser;
import tv.huan.system.service.ISysUserService;

/**
 * 审核小组Controller
 * 
 * @author admin
 * @date 2020-04-02
 */
@Controller
@RequestMapping("/media/team")
public class TeamController extends BaseController
{
    private String prefix = "media/team";

    @Autowired
    private ITeamService teamService;

    @Autowired
    private ISysUserService sysUserService;

    @RequiresPermissions("media:team:view")
    @GetMapping()
    public String team()
    {
        return prefix + "/team";
    }

    /**
     * 查询审核小组列表
     */
    @RequiresPermissions("media:team:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Team team)
    {
        startPage();
        List<Team> list = teamService.selectTeamList(team);
        return getDataTable(list);
    }

    /**
     * 新增审核小组
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存审核小组
     */
    @RequiresPermissions("media:team:add")
    @Log(title = "审核小组", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Team team)
    {
        return toAjax(teamService.insertTeam(team));
    }

    /**
     * 修改审核小组
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        Team team = teamService.selectTeamById(id);
        mmap.put("team", team);
        return prefix + "/edit";
    }

    /**
     * 修改保存审核小组
     */
    @RequiresPermissions("media:team:edit")
    @Log(title = "审核小组", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Team team)
    {
        return toAjax(teamService.updateTeam(team));
    }

    /**
     * 删除审核小组
     */
    @RequiresPermissions("media:team:remove")
    @Log(title = "审核小组", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(teamService.deleteTeamByIds(ids));
    }

    /**
     * 查询未分配的审核人员
     */
    @RequiresPermissions("media:team:list")
    @PostMapping("/noAssignUserList")
    @ResponseBody
    public TableDataInfo noAssignUserList(SysUser user)
    {
        startPage();
        List<SysUser> list = sysUserService.noAssignUserList(user);
        return getDataTable(list);
    }

    /**
     * 分配审核人员
     */
    @RequiresPermissions("media:team:assignUser")
    @GetMapping("/assignUser/{teamId}")
    public String assignUser(@PathVariable("teamId") Integer teamId, ModelMap mmap)
    {
        mmap.put("teamId", teamId);
        return prefix + "/assignUser";
    }

    /**
     * 分配审核人员（保存）
     */
    @RequiresPermissions("media:team:assignUser")
    @PostMapping("/assignUser")
    @ResponseBody
    public AjaxResult assignUser(Integer teamId, String userIds)
    {
        try {
            this.teamService.assignUser(teamId, userIds);
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success("分配成功");
    }

    /**
     * 已分配审核人员列表
     */
    @RequiresPermissions("media:team:assignUser")
    @GetMapping("/auditUserList/{teamId}")
    public String auditUserList(@PathVariable("teamId") Integer teamId, ModelMap mmap)
    {
        mmap.put("teamId", teamId);
        return prefix + "/auditUserList";
    }

    /**
     * 查询已经分配的人员列表
     */
    @RequiresPermissions("media:team:assignUser")
    @PostMapping("/auditUserList/{teamId}")
    @ResponseBody
    public TableDataInfo auditUserList(@PathVariable("teamId")Integer teamId, SysUser user)
    {
        startPage();
        List<SysUser> list = sysUserService.auditUserList(teamId,user.getUserName());
        return getDataTable(list);
    }

    /**
     * 删除已经分配人员（保存）
     */
    @RequiresPermissions("media:team:assignUser")
    @PostMapping("/deleteAuditUserList")
    @ResponseBody
    public AjaxResult deleteAuditUserList(Integer teamId, String userIds)
    {
        try {
            this.teamService.deleteAuditUserList(teamId, userIds);
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success("删除成功");
    }
}
